<template>
	<view class="">
		<navigationBar :titleName="'我的消息'"></navigationBar>
		<view class="setPadding setFontColor2">
			<view v-if="msgData"  class="">
				<view class="settextRight">
				
					<text @click="clearAllMsg()" class="fontSize setcolor">清除未读</text>
				</view>
				<view  @click="toDetail(item)" class="setPadding setRadius setBgfff  setBasestyle"
					v-for="(item,index) in msgData" :key="index">
					<view class="sethorizontalbetween">
						<text class="fontSize">{{item.title}}</text>
						<text class="setcolor setfontSize2" v-if="item.readFlag==0">未读</text>
						<text class="setfontSize2" v-else>已读</text>
					</view>
					<view class="setMarginTop fontSize setFontColor1">
						{{item.content}}
					</view>
					<view class="settextRight">
						<!-- $u.timeFormat(item.createTime, 'yyyy年mm月dd日') -->
						<text class="setfontSize2">{{item.createTime}}</text>
					</view>
				
				</view>
			</view>
			<view v-else class="">
				<noData text="暂时还没有消息~" ></noData>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 记录消息
				msgData: null,
				// 
			}
		},
		onLoad() {
			// 删除app上显示数字
			 // #ifdef APP-PLUS 
			plus.runtime.setBadgeNumber(0);
			 // #endif 
		},
		beforeMount() {
			this.getUsersMsg()
		},
		methods: {
			// 清除全部未读
			clearAllMsg(){
				
				this.$app.request({
					url: this.$baseApp.api.user.readAll,
					method: 'POST',
					dataType: 'json',
					success: res => {
						// console.log('这里是清除所有信息消息', res)
						// this.msgData=null
						
							
						this.msgData.forEach(item=>{
							item.readFlag=null
						})
					},
					error: (res) => {
						console.log("res", res)
					},
					complete: res => {
						
				
					}
				});
			},
			toDetail(item) {
				// 跳转至招标详情
				uni.navigateTo({
					url: '/pagesIndex/detail?id=' + item.busId,
					success: () => {
						// 设置成已读
						this.readSingeMsg(item)
					}
				});
				// 该消息设置成已读

			},
			// 单条消息设置成已读
			readSingeMsg(item) {
				let param = {
					id: item._id
				}
				this.$app.request({
					url: this.$baseApp.api.user.readOne,
					data: param,
					method: 'POST',
					dataType: 'json',
					success: res => {
						// console.log('这里是阅读单条消息', res)
						this.getUsersMsg()
					},
					error: (res) => {
						console.log("res", res)
					},
					complete: res => {


					}
				});
			},
			getUsersMsg() {
				let param = {
					page: 1,
					pageSize: 10
				}
				this.$app.request({
					url: this.$baseApp.api.user.userMessage,
					data: param,
					method: 'GET',
					dataType: 'json',
					success: res => {
						// console.log("这里是查询用户消息", res)
						if (res.data.content.length >= 1) {
							this.msgData = []

							res.data.content.forEach(item => {
								if (item.createTime) {
									item.createTime = uni.$u.timeFormat(item.createTime, 'yyyy-mm-dd')
								}
								this.msgData.push(item)
							})

						}
						// console.log("msgData",this.msgData)

					},
					error: (res) => {
						console.log("res", res)
					},
					complete: res => {


					}
				});
			}
		}
	}
</script>

<style>
	.fontSize{
		font-size: 30rpx;
	}
</style>
